﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac.Integration.Web;
using Autofac;
using Autofac.Integration.Web.Mvc;

namespace JsonRestObjectContext
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication, IContainerProviderAccessor
    {
        // Provider that holds the application container.
        static IContainerProvider _containerProvider;

        // Instance property that will be used by Autofac HttpModules
        // to resolve and inject dependencies.
        public IContainerProvider ContainerProvider
        {
            get { return _containerProvider; }
        }


        public class OperationAuthorizedConstraint : System.Web.Routing.IRouteConstraint
        {

            public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
            {
                if (routeDirection == RouteDirection.IncomingRequest)
                {
                    if (values["action"] == null)
                    {
                        values["action"] = httpContext.Request.HttpMethod;
                    }

                    //Implement your own authorization here
                    return true;

                }
                return true;
            }

        }


        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "REST_OBJ_CXT",
                "Data/{store}/{id}",
                new { controller = "ObjectContextJsonRest", id = UrlParameter.Optional },
                new { httpMethod = new OperationAuthorizedConstraint() });

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Home", id = UrlParameter.Optional } // Parameter defaults
            );



        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            // Build up your application container and register your dependencies.
            var builder = new ContainerBuilder();
            builder.RegisterType<Models.MyModelContainer>().As<System.Data.Objects.ObjectContext>();
            builder.RegisterType<System.Web.Script.Serialization.JavaScriptSerializer>();
            builder.RegisterControllers(System.Reflection.Assembly.GetExecutingAssembly()).PropertiesAutowired();


            // Once you're done registering things, set the container
            // provider up with your registrations.
            _containerProvider = new ContainerProvider(builder.Build());
            // Set the controller factory using the container provider.
            ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(ContainerProvider));


            RegisterRoutes(RouteTable.Routes);
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);

        }
    }
}